package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.AppCompatButton;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.home.wrap.PopManager;
import cn.com.dhc.danlu.util.ACache;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.hd.mall.web.webservices.entity.request.merch.MerchListRequest;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * 品牌下拉
 */
public class CustomerSpinner extends AppCompatButton implements OnItemClickListener {
    /**
     * 初始页数
     */
    private static final int DEFAULT_PAGE = 1;
    /**
     * 低价传递参数
     */
    private static final String LOW_PRICE = "0";
    /**
     * 高价传递参数
     */
    private static final String HIGH_PRICE = "1";
    /**
     * 按名字排序 0按名字 1 按价格
     */
    private static final String NAME_SORT = "0";
    private static final String PRICE_SORT = "1";
    /**
     * 请求参数类
     */
    private MerchListRequest goodsform = new MerchListRequest();
    /**
     * 商品类型   默认为选择白酒
     */
    private String categroyCode = null;
    /**
     * 商品平铺列表
     */
    private GoodsGridView goodsGridView;
    /**
     * 品牌码
     */
    private String brand;
    private ArrayList<String> list;// ArrayList<String> list存储所要显示的数据
    private String text;
    /**
     * 弹出框
     */
    private PopManager mPopManager;
    /**
     * 获取父控件宽度
     */
    private LinearLayout llytSpinner;
    /**
     * 商品搜索tv
     */
    private TextView txtSearchView;

    private ImageView imgCountSearch;
    private ImageView imgPriceSearch;
    private TextView llytCountSearch_txt;
    private TextView llytPriceSearch_txt;
    private TextView txtFilter;
    private View viewPrice;
    private View viewCount;
    private View viewFilter;

    private int mDefaultSelectedPos = 0;

    private int sectionTmp = -1;

    public CustomerSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomerSpinner(Context context) {
        super(context);
    }

    public CustomerSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // 如果视图定义了OnClickListener监听器，调用此方法来执行
//    @Override
//    public boolean performClick () {
//        if (null != mPopManager) {
//            mPopManager.showPop (this);
//            return true;
//        }
//
//        mPopManager = new PopManager (getContext ());
//        //        mPopManager.setDefaultSection (sectionTmp);
//        mPopManager.showPopIndex (this, SerializeCache.getInstance ().getCategoryIndex ());
//        return true;
//    }

    /**
     * homeActivity force this method call
     */
    public void forcedToScreen(String code, String brandId, String description) {
        setText(description);
        reloadByConditions(code, brandId);
    }

    public void setPopSection(int section) {
        if (null != mPopManager) {
            mPopManager.setDefaultSection(section);
        }
        sectionTmp = section;
    }

    @Override
    public void onItemClick(AdapterView<?> view, View itemView, int position, long id) {
        setText(list.get(position));
        if (mPopManager != null) {
            mPopManager.dismissPop();
            mDefaultSelectedPos = position;
            mPopManager.setDefaultSection(mDefaultSelectedPos);
        }
        if (list.get(position).equals(CommonConst.IntentKey.WINE_WORD)) {
            // 设置品牌类型为白酒
            categroyCode = CommonConst.IntentKey.WINE_CODE;
            // 如果品牌等于茶码
        } else if (list.get(position).equals(CommonConst.IntentKey.TEA_WORD)) {
            // 设置品牌类型为茶
            categroyCode = CommonConst.IntentKey.TEA_CODE;
        } else if (list.get(position).equals(CommonConst.IntentKey.RED_WINE_WORD)) {
            // 设置品牌类型为红酒
            categroyCode = CommonConst.IntentKey.RED_WINE_CODE;
        } else if (list.get(position).equals(CommonConst.IntentKey.FOREIGN_WINE_WORD)) {
            // 设置品牌类型为洋酒
            categroyCode = CommonConst.IntentKey.FOREIGN_CODE;
        } else if (list.get(position).equals(CommonConst.IntentKey.BEER_WORD)) {
            // 设置品牌类型为洋酒
            categroyCode = CommonConst.IntentKey.BEER_CODE;
        } else if (list.get(position).equals(CommonConst.IntentKey.OTHER_WINE_WORD)) {
            // 设置品牌类型为饮料(品类优化钱，为其它)
            categroyCode = CommonConst.IntentKey.DRINKS_CODE;
        }
        reloadByConditions(categroyCode, null);
    }

    public void reloadByConditions(String code, String brandId) {
        // 设置默认综合排序
        setDefaultSort();
        // CategoryActivity.COUNT = false;
        ACache.get(getContext()).put("count", "1"); // modify why
        CommonUtils.clearAllCgrPrtBrd(getContext());
        // 每一次点击就清空搜索框
        txtSearchView.setText(R.string.good_info_search_btnok);
        // 设置名称排序
        goodsform.setSortField(NAME_SORT);
        // 升序
        // goodsform.setSortType(HIGH_PRICE);
        goodsform.setSortType(LOW_PRICE);
        // 设置返回数据页数为默认
        goodsform.setPage(DEFAULT_PAGE);
        // 设置返回行数
        goodsform.setRows(CommonConst.LIST_NUM);
        // 设置token
        goodsform.setToken(agent.getToken());
        categroyCode = code;
        clearGoodsForm();
        // 设置类目
        goodsform.setMerchCategoryCode(categroyCode);

        if (!TextUtils.isEmpty(brandId)) {
            goodsform.setMerchBrandId(brandId);
        }
        // 设置返回数据页数为默认
        goodsform.setPage(DEFAULT_PAGE);
        // 设置商品类型
        // goodsform.setMerchandiseType(merchandiseType);
        goodsGridView.getCateList(true, goodsform);
        ACache.get(getContext()).put("isClick", "0");
        // CategoryActivity.IS_CLICK = true;
    }

    public ArrayList<String> getList() {
        return list;
    }

    public void setList(ArrayList<String> list) {
        this.list = list;
    }

    public void setGoodsGridView(GoodsGridView goodsGridView) {
        this.goodsGridView = goodsGridView;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public void setLinearLayout(LinearLayout llytSpinner) {
        this.llytSpinner = llytSpinner;
    }

    public String getMerchandiseType() {
        return this.categroyCode;
    }

    public MerchListRequest getGoodsform() {
        return goodsform;
    }

    public void setGoodsform(MerchListRequest goodsform) {
        this.goodsform = goodsform;
    }

    public void setSearchTextView(TextView view) {
        txtSearchView = view;
    }

    /**
     * 清除一下请求参数类中参数
     */
    private void clearGoodsForm() {
        // 清除品牌码
        goodsform.setMerchBrandId(null);
        goodsform.setMerchPropertyValueCodeList(null);
        // 清除品牌类型
        // goodsform.setMerchandiseType("");
        // 清除高价
        goodsform.setHighPrice(null);
        // 清除低价
        goodsform.setLowPrice(null);
        // 清除商品名称
        goodsform.setMerchName(null);
    }

    public String getCategroyCode() {
        return categroyCode;
    }

    public void setCategroyCode(String categroyCode) {
        this.categroyCode = categroyCode;
    }

    /**
     * 清空后设置默认综合排序
     */
    private void setDefaultSort() {
        ACache aCache = ACache.get(getContext());
        // 默认综合排序
        imgCountSearch.setVisibility(View.VISIBLE);
        imgPriceSearch.setVisibility(View.INVISIBLE);
        llytPriceSearch_txt.setTextColor(Color.BLACK);
        llytCountSearch_txt.setTextColor(Color.RED);
        txtFilter.setTextColor(Color.BLACK);
        // 综合tab下的红色线显示
        viewPrice.setVisibility(View.INVISIBLE);
        viewCount.setVisibility(View.VISIBLE);
        viewFilter.setVisibility(View.INVISIBLE);
        // 设置排序图片
        imgCountSearch.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.category_sort_up));
    }

    public void setImgCountSearch(ImageView imgCountSearch) {
        this.imgCountSearch = imgCountSearch;
    }

    public void setImgPriceSearch(ImageView imgPriceSearch) {
        this.imgPriceSearch = imgPriceSearch;
    }

    public void setLlytCountSearch_txt(TextView llytCountSearch_txt) {
        this.llytCountSearch_txt = llytCountSearch_txt;
    }

    public void setLlytPriceSearch_txt(TextView llytPriceSearch_txt) {
        this.llytPriceSearch_txt = llytPriceSearch_txt;
    }

    public void setTxtFilter(TextView txtFilter) {
        this.txtFilter = txtFilter;
    }

    public void setViewPrice(View viewPrice) {
        this.viewPrice = viewPrice;
    }

    public void setViewCount(View viewCount) {
        this.viewCount = viewCount;
    }

    public void setViewFilter(View viewFilter) {
        this.viewFilter = viewFilter;
    }
}